import { Injectable } from '@nestjs/common';


import { InjectModel } from '@nestjs/mongoose';


import { Model } from 'mongoose';


import { IBanner } from 'src/types/banner.interface';


//通过@Injectable装饰器申明这是一个可注入的服务类
@Injectable()
export class BannerService {



    /**
     * 参数装饰器,BannerTable 这个名字要和app.module.ts中的MongooseModule.forFeature 定义的名字一样
     * IIBanner 是定义好
     * @param bannelModel 
     */
    constructor(@InjectModel('BannerTable') private bannelModel: Model<IBanner>) {

    }




    async findService(queryCondition): Promise<IBanner[]> {

        return this.bannelModel.find(queryCondition).exec();


    }

    async createService(requestParamsBody: IBanner | IBanner[]): Promise<IBanner | IBanner[]> {
        console.log('requestParamsBody====', requestParamsBody);

        //如果请求体是一个数组
        if (Array.isArray(requestParamsBody)) {
            return this.bannelModel.insertMany(requestParamsBody);
        }
        else {
            return new this.bannelModel(requestParamsBody).save();
        }

    }
}
